package vashion.azeroth.api.controller.item;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import vashion.azeroth.api.controller.base.BaseController;
import vashion.azeroth.contant.AzerothConstants;
import vashion.azeroth.contant.CommonErrorEnum;
import vashion.azeroth.contant.GenericErrorEnum;
import vashion.azeroth.contant.ItemErrorEnum;
import vashion.azeroth.core.item.pojo.ItemDO;
import vashion.azeroth.core.item.pojo.ItemSkuDO;
import vashion.azeroth.core.item.query.ItemQueryDO;
import vashion.azeroth.core.manager.item.BaseItemManager;
import vashion.azeroth.core.manager.item.BaseItemSkuManager;
import vashion.azeroth.core.manager.item.ItemReadManger;
import vashion.azeroth.core.manager.item.ItemWriteManager;
import vashion.azeroth.core.manager.personal.PersonalMemberManager;
import vashion.azeroth.core.personal.pojo.PersonalMember;
import vashion.azeroth.core.result.ApiResult;
import vashion.azeroth.core.service.item.ItemRecommendService;

@Controller
@RequestMapping("/api/item")
public class ItemController<T> extends BaseController {

	private final static Logger log = LoggerFactory.getLogger(ItemController.class);

	@Autowired
	private BaseItemManager baseItemManager;
	@Autowired
	private ItemReadManger itemReadManger;
	@Autowired
	private ItemRecommendService itemRecommendService;
	@Autowired
	private ItemWriteManager itemWriteManager;
	@Autowired
	private BaseItemSkuManager baseItemSkuManager;
	@Autowired
	private PersonalMemberManager memberManager;

	@RequestMapping("getItemList.htm")
	@ResponseBody
	public ApiResult<List<ItemDO>> getItemList(@ModelAttribute("member") PersonalMember member,
			@ModelAttribute("itemQueryDO") ItemQueryDO itemQueryDO,
			@RequestParam(value = "isMyInvitor", required = false, defaultValue = "true") boolean isMyInvitor,
			@RequestParam(value = "brandIds", required = false) String brandIds,
			@RequestParam(value = "categoryIds", required = false) String categoryIds,
			@RequestParam(value = "curno", required = false) String curno) {
		return itemReadManger.getItemListforSupplierAndAgent(member, itemQueryDO, isMyInvitor, brandIds, categoryIds,
				curno);
	}

	@RequestMapping("getSameCodeItemList.htm")
	@ResponseBody
	public ApiResult<List<ItemDO>> getSameCodeItemList(@ModelAttribute("member") PersonalMember member,
			@ModelAttribute("itemQueryDO") ItemQueryDO itemQueryDO,
			@RequestParam(value = "curno", required = false) String curno) {
		return itemReadManger.getSameCodeItemListForAgent(member, itemQueryDO, curno);
	}

	@RequestMapping("getRecommendWord.htm")
	@ResponseBody
	public ApiResult<List<String>> getRecommendWord(@RequestParam(value = "word") String word) {
		return itemReadManger.getRecommendTitle(word);
	}

	@RequestMapping("recommendItemList.htm")
	@ResponseBody
	public ApiResult<List<ItemDO>> recommendItem(@RequestParam(value = "itemId", required = false) Long itemId,
			@RequestParam(value = "userId", required = false) Long userId) {
		// 商品没有推荐
		// if (null != itemId && itemId > 0) {
		// return itemRecommendService.recommendItemByItemId(itemId);
		// } else
		if (null != userId && userId > 0) {
			return itemRecommendService.recommendItemByUserId(userId);
		} else {
			return ApiResult.newErrorResult(CommonErrorEnum.SYSTEM_ERROR.getErrorCode(),
					CommonErrorEnum.SYSTEM_ERROR.getErrorMessage());
		}
	}

	@RequestMapping("getItemDetail.htm")
	@ResponseBody
	public ApiResult<ItemDO> getItemDetail(@ModelAttribute("member") PersonalMember member,
			@RequestParam(value = "itemId", required = false) long itemId,
			@RequestParam(value = "needSku", required = false) boolean needSku,
			@RequestParam(value = "curno", required = false) String curno) {

		if (itemId <= 0) {

			return ApiResult.newErrorResult(ItemErrorEnum.ITEM_ARG_ERROR.getErrorCode(),
					ItemErrorEnum.ITEM_ARG_ERROR.getErrorMessage());
		}
		return itemReadManger.getItemDetailforSupplierAndAgent(member, itemId, needSku, curno);
	}

	@RequestMapping("publishItem.htm")
	@ResponseBody
	public ApiResult<ItemDO> publishItem(@ModelAttribute("member") PersonalMember member,
			@ModelAttribute("itemDO") ItemDO itemDO,
			@RequestParam(value = "miniSkuList", required = false) String miniSkuList) {

		if (null == itemDO) {
			return ApiResult.newErrorResult(ItemErrorEnum.ITEM_ARG_ERROR.getErrorCode(),
					ItemErrorEnum.ITEM_ARG_ERROR.getErrorMessage());
		}
		// 检查用户是否有权发布商品
		member = memberManager.get(member.getMemberId());
		if (null == member) {
			return ApiResult.newErrorResult(GenericErrorEnum.NOT_EXIST.getErrorCode(),
					GenericErrorEnum.NOT_EXIST.getErrorMessage());
		} else if (AzerothConstants.PersonalMember.MemberType.supplier != member.getType()) {
			return ApiResult.newErrorResult(ItemErrorEnum.ITEM_NO_PUBLISH_AUTHORITY.getErrorCode(),
					ItemErrorEnum.ITEM_NO_PUBLISH_AUTHORITY.getErrorMessage());
		}
		// 设置发布者的ID
		itemDO.setUserId(member.getMemberId());
		itemDO.setMemberName(member.getMemberName());
		List<ItemSkuDO> skuDOList = baseItemSkuManager.parseSku(miniSkuList, itemDO);
		ApiResult<Boolean> publishResult = itemWriteManager.publishItem(itemDO, skuDOList, null, member.getMemberId());
		if (publishResult.isSuccess() && publishResult.getData()) {
			return ApiResult.newSuccessResult();
		} else {
			return ApiResult.newErrorResult(publishResult.getErrorCode(), publishResult.getErrorMsg());
		}
	}

	@RequestMapping("updateItem.htm")
	@ResponseBody
	public ApiResult<Boolean> updateItem(@ModelAttribute("itemDO") ItemDO itemDO,
			@RequestParam(value = "miniSkuList", required = false) String miniSkuList,
			@ModelAttribute("member") PersonalMember member) {

		if (null == itemDO) {
			return ApiResult.newErrorResult(ItemErrorEnum.ITEM_ARG_ERROR.getErrorCode(),
					ItemErrorEnum.ITEM_ARG_ERROR.getErrorMessage());
		}
		if (null == member) {
			return ApiResult.newErrorResult(GenericErrorEnum.NOT_EXIST.getErrorCode(),
					GenericErrorEnum.NOT_EXIST.getErrorMessage());
		}
		ItemDO originItemDO = itemReadManger.getItemDetail(itemDO.getId(), false).getData();
		if (member.getMemberId() != originItemDO.getUserId()) {
			return ApiResult.newErrorResult(ItemErrorEnum.ITEM_NOT_OWER.getErrorCode(),
					ItemErrorEnum.ITEM_NOT_OWER.getErrorMessage());
		}
		List<ItemSkuDO> skuDOList = baseItemSkuManager.parseSku(miniSkuList, itemDO);
		if (itemWriteManager.updateItem(itemDO, null, member.getMemberId()).isSuccess()) {
			return ApiResult.newSuccessResult(baseItemSkuManager.updateSkuList(skuDOList));
		} else {
			return ApiResult.newErrorResult(ItemErrorEnum.SYSTEM_ERROR.getErrorCode(),
					ItemErrorEnum.SYSTEM_ERROR.getErrorMessage());
		}

	}

	/**
	 * 下架商品
	 *
	 * @param itemDO
	 * @return
	 */
	@RequestMapping("downItem.htm")
	@ResponseBody
	public ApiResult<Boolean> downItem(@ModelAttribute("ItemDO") ItemDO itemDO,
			@ModelAttribute("member") PersonalMember member) {
		ItemDO originItemDO = baseItemManager.get(itemDO.getId());
		if (null == originItemDO) {
			return ApiResult.newErrorResult(ItemErrorEnum.NOT_EXITS.getErrorCode(),
					ItemErrorEnum.NOT_EXITS.getErrorMessage());
		}
		if (null == member) {
			return ApiResult.newErrorResult(GenericErrorEnum.NOT_EXIST.getErrorCode(),
					GenericErrorEnum.NOT_EXIST.getErrorMessage());
		} else if (member.getMemberId() != originItemDO.getUserId()) {
			return ApiResult.newErrorResult(ItemErrorEnum.ITEM_NOT_OWER.getErrorCode(),
					ItemErrorEnum.ITEM_NOT_OWER.getErrorMessage());
		}
		return itemWriteManager.deleteItem(itemDO.getId(), member.getMemberId());
	}

	/**
	 * 恢复商品
	 *
	 * @param itemDO
	 * @return
	 */
	@RequestMapping("resumeItem.htm")
	@ResponseBody
	public ApiResult<Boolean> resumeItem(@ModelAttribute("ItemDO") ItemDO itemDO,
			@ModelAttribute("member") PersonalMember member) {
		ItemDO originItemDO = baseItemManager.get(itemDO.getId());
		if (null == originItemDO) {
			return ApiResult.newErrorResult(ItemErrorEnum.NOT_EXITS.getErrorCode(),
					ItemErrorEnum.NOT_EXITS.getErrorMessage());
		}
		if (null == member) {
			return ApiResult.newErrorResult(GenericErrorEnum.NOT_EXIST.getErrorCode(),
					GenericErrorEnum.NOT_EXIST.getErrorMessage());
		} else if (member.getMemberId() != originItemDO.getUserId()) {
			return ApiResult.newErrorResult(ItemErrorEnum.ITEM_NOT_OWER.getErrorCode(),
					ItemErrorEnum.ITEM_NOT_OWER.getErrorMessage());
		}
		return itemWriteManager.resumeItem(itemDO.getId(), member.getMemberId());
	}
	
}
